package com.lywz.web.action.admin.food;

import java.io.File;
import java.util.List;

import com.lywz.biz.common.entity.goodfood.Food;
import com.lywz.biz.common.enums.FoodTypeEnum;
import com.lywz.core.exception.SSException;
import com.lywz.core.fileUpload.UploadFile;
import com.lywz.core.log.LogClerk;
import com.lywz.core.web.WebConstants;
import com.lywz.web.struts.GZAbstracterAction;

/**
 * @author qianqi yangbolin
 * @date 2010-4-29
 * @version 1.0
 */
public class FoodBackOperateAction extends GZAbstracterAction {

    private static final long serialVersionUID = -9120989153294200379L;

    private Food foodInstance;

    private Integer foodId;

    private FoodTypeEnum foodTypeObj;

    private List<FoodTypeEnum> foodTypeList = FoodTypeEnum.getList();

    //美食列表
    private List<Food> foodList;

    //添加食品是的提示列表1 用于展示该类食品前N名在首页展示的权重
    private List<Food> showInMainList;

    //添加食品是的提示列表2 用于展示该类食品前N名在二级页面展示的权重
    private List<Food> showInSecondList;

    //食物类别
    private int foodType;

    //图像文件
    private File imageFile;

    //文件名称
    private String imageFileFileName;

    /**
     * 添加美食
     * 
     * @return
     */
    public String addNewFood() {

        String imageUrl = "";
        try {
            imageUrl = saveImageFile(imageFile, WebConstants.foodImagePath, imageFileFileName, 0, 0);
        } catch (SSException e1) {
            LogClerk.sysout.error(e1);
            return ERROR;
        }
        foodType = foodInstance.getFoodTypeId();
        foodInstance.setFoodTypeName(FoodTypeEnum.getNameById(foodType));
        foodInstance.setImageUrl(imageUrl);
        try {
            if (foodService.insertNewFood(foodInstance)) {
                return "actionToList";
            } else {
                return ERROR;
            }
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 跳转到添加食物的页面 需要获得两个该食物类别的相关列表
     * 
     * @return
     */
    public String toAddFood() {

        showInMainList = foodService.getOtherFoodTypeList(foodType, 6);
        showInSecondList = foodService.getTypedFoodTypeList(foodType, 6);
        return "goToAddFoodPage";
    }

    /**
     * 返回某种特定类型的美食的List(按照添加的时间倒序)
     * 
     * @return
     */
    public String getTypedFoodListByFoodType() {
        try {
            foodList = foodService.getAllTypedFoodListByAddTime(foodType);
            return "toSomeTypedFood";
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 根据foodType的值判断是返回特色美食列表还是香茶美酒列表
     * 如果foodType为（1~5）则返回特色美食列表，如果foodType为（6.7）则返回香茶美酒列表
     * 
     * @return
     */
    public String getSpecialOrDrinkTeaList() {
        try {
            foodList = foodService.getSpecialOrDrinkTeaListOrderByTime(foodType < 6);
            return "toSpecialOrDrinkTeaPage";
        } catch (SSException e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 根据id 删除一个食物的实体
     * 
     * @return
     */
    public String deleteOneFood() {
        try {
            if (foodService.deleteOneFoodById(foodId)) return "actionToList";
            else {
                return ERROR;
            }
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }

    }

    /**
     * 修改一个美食
     * @return
     */
    public String updateOneFood() {
        
        String imageUrl = "";
        try {
            imageUrl = saveImageFile(imageFile, WebConstants.foodImagePath, imageFileFileName, 0, 0);
        } catch (SSException e1) {
            LogClerk.sysout.error(e1);
            return ERROR;
        }
        
        foodType = foodInstance.getFoodTypeId();
        foodInstance.setFoodTypeName(FoodTypeEnum.getNameById(foodType));
        foodInstance.setImageUrl(imageUrl);
        try {
            if (foodService.updateOneFood(foodInstance)) return "actionToList";
            else return ERROR;
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 到修改美食页面
     * 
     * @return
     */
    public String toUpdateFoodPage() {
        try {
            foodInstance = foodService.getOneFood(foodId);
            showInMainList = foodService.getOtherFoodTypeList(foodType, 6);
            showInSecondList = foodService.getTypedFoodTypeList(foodType, 6);
            return "toUpdateFoodPage";
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 跳转到显示一个具体美食页面
     * 
     * @return
     */
    public String showOneFood() {
        try {
            foodInstance = foodService.getOneFood(foodId);
            return "toShowOneFood";
        } catch (SSException e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 保存上传图片并返回路径
     * 
     * @param imageFile
     * @param dir
     * @param imageFileFileName
     * @param imageWith
     * @param imageHeight
     * @return
     * @throws SSException
     */
    public String saveImageFile(File imageFile, String dir, String imageFileFileName,
            int imageWith, int imageHeight) throws SSException {

        UploadFile files = new UploadFile(imageFile, dir, imageFileFileName, imageWith,
                imageHeight, request);

        String path = defaultFileUpload.uploadOneFile(files);

        return path;
    }

    //////////////////**************  setter getter *****************/////////////////////
    public List<FoodTypeEnum> getFoodTypeList() {
        return foodTypeList;
    }

    public void setFoodTypeList(List<FoodTypeEnum> foodTypeList) {
        this.foodTypeList = foodTypeList;
    }

    public void setImageFile(File imageFile) {
        this.imageFile = imageFile;
    }

    /**
     * @return the imageFile
     */
    public File getImageFile() {
        return imageFile;
    }

    /**
     * @return the imageFileFileName
     */
    public String getImageFileFileName() {
        return imageFileFileName;
    }

    public FoodTypeEnum getFoodTypeObj() {
        return foodTypeObj;
    }

    public void setFoodTypeObj(FoodTypeEnum foodTypeObj) {
        this.foodTypeObj = foodTypeObj;
    }

    public int getFoodType() {
        return foodType;
    }

    public void setFoodType(int foodType) {
        this.foodType = foodType;
    }

    public List<Food> getShowInMainList() {
        return showInMainList;
    }

    public List<Food> getShowInSecondList() {
        return showInSecondList;
    }

    public void setShowInMainList(List<Food> showInMainList) {
        this.showInMainList = showInMainList;
    }

    public void setShowInSecondList(List<Food> showInSecondList) {
        this.showInSecondList = showInSecondList;
    }

    public List<Food> getFoodList() {
        return foodList;
    }

    public void setFoodList(List<Food> foodList) {
        this.foodList = foodList;
    }

    public Food getFoodInstance() {
        return foodInstance;
    }

    public Integer getFoodId() {
        return foodId;
    }

    public void setFoodInstance(Food foodInstance) {
        this.foodInstance = foodInstance;
    }

    public void setFoodId(Integer foodId) {
        this.foodId = foodId;
    }

    public void setImageFileFileName(String imageFileFileName) {
        this.imageFileFileName = imageFileFileName;
    }

}
